﻿using IPlugin;
using IPlugin.Attributes;
using NLog;
using S7CommPlusDriver;

namespace PLC.SiemensS7CommPlus
{
    [DriverSupported("SiemensS7PlusDriver")]
    [DriverInfo("SiemensS7PlusDriver", "V1.0.0", "湖州精确科技有限公司")]
    public class SiemensS7PlusDriver : IDriver
    {
        private S7CommPlusConnection _plc;
        private static Logger _logger = LogManager.GetLogger(nameof(SiemensS7PlusDriver));
        private readonly string _device;


        private bool _isConnected;
        public bool IsConnected => _isConnected;

        #region 配置参数
        [ConfigParameter("设备Id")]
        public int DeviceId { get; set; }
        [ConfigParameter("超时时间ms")]
        public int TimeOut { get; set; } = 3000;
        [ConfigParameter("最小通讯周期ms")]
        public int MinPeriod { get; set; } = 3000;
        [ConfigParameter("IP地址")]
        public string IpAddress { get; set; } = "127.0.0.1";

        [ConfigParameter("密码")]
        public string Password { get; set; } = "";
        [ConfigParameter("端口号")]
        public int Port { get; set; } = 102;
        #endregion


        public SiemensS7PlusDriver(string device)
        {
            _device = device;
            _logger.Info($"Device:[{_device}],Create()");
        }
        public bool Close()
        {
            try
            {
                _logger.Info($"Device:[{_device}],Close()");
                _plc.Disconnect();
                _isConnected = false;
                return !IsConnected;
            }
            catch (Exception ex)
            {
                _logger.Error(ex, $"Device:[{_device}],Close(),Error");
                return false;
            }
        }

        public bool Connect()
        {
            try
            {
                _logger.Info($"Device:[{_device}],Connect()");

                _plc = new S7CommPlusConnection();
                int res = _plc.Connect(IpAddress, Password, TimeOut);
                _isConnected = false;
                if (res == 0)
                {
                    _isConnected = true;
                }
            }
            catch (Exception ex)
            {
                _logger.Error($"西门子Device:[{_device}],Connect() Error", ex);
                return false;
            }

            return IsConnected;
        }

        public DriverReturnValueModel Read(DriverAddressIoArgModel ioArg)
        {
            throw new NotImplementedException();
        }

        public Task<RpcResponse> WriteAsync(string RequestId, string Method, DriverAddressIoArgModel ioArg)
        {
            throw new NotImplementedException();
        }

        public void Dispose()
        {
            try
            {
                _logger.Info($"Device:[{_device}],Dispose()");

                _plc = null;

                // Suppress finalization.
                GC.SuppressFinalize(this);
            }
            catch (Exception ex)
            {
                _logger.Error(ex, $"Device:[{_device}],Dispose(),Error");
            }
        }
    }
}
